package com.leo;

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * @ClassName ToggleButtonDemo
 * @Description Timeline 时间轴动画
 * @Author wangss
 * @date 2020.07.07 00:19
 * @Version 1.0
 */
public class Timeline3Demo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox(50);
        ToggleGroup tg = new ToggleGroup();

        ToggleButton tb1 = new ToggleButton("播放");
        tb1.setToggleGroup(tg);

        ToggleButton tb2 = new ToggleButton("暂停");
        tb2.setToggleGroup(tg);

        ToggleButton tb3 = new ToggleButton("停止");
        tb3.setToggleGroup(tg);

        ToggleButton tb4 = new ToggleButton("跳转");
        tb4.setToggleGroup(tg);

        Rectangle rec = new Rectangle(100, 100, Color.RED);

        // 暂停1s旋转1s
        KeyValue kv1 = new KeyValue(rec.rotateProperty(), 0);
        KeyFrame kf1 = new KeyFrame(Duration.seconds(1), kv1);
        KeyValue kv2 = new KeyValue(rec.rotateProperty(), 360);
        KeyFrame kf2 = new KeyFrame(Duration.seconds(2), kv2);

        // x轴移动
        KeyValue kv3 = new KeyValue(rec.translateXProperty(), 0);
        KeyFrame kf3 = new KeyFrame(Duration.seconds(2), kv3);
        KeyValue kv4 = new KeyValue(rec.translateXProperty(), 300);
        KeyFrame kf4 = new KeyFrame(Duration.seconds(6), kv4);

        // 再旋转1s
        KeyValue kv5 = new KeyValue(rec.rotateProperty(), 360);
        KeyFrame kf5 = new KeyFrame(Duration.seconds(6), kv5);
        KeyValue kv6 = new KeyValue(rec.rotateProperty(), 720);
        KeyFrame kf6 = new KeyFrame(Duration.seconds(7), kv6);

        // 暂停1s
        KeyValue kv7 = new KeyValue(rec.rotateProperty(), 720);
        KeyFrame kf7 = new KeyFrame(Duration.seconds(8), kv7);

        Timeline timeline = new Timeline();
        timeline.getKeyFrames().addAll(kf1, kf2, kf3, kf4, kf5, kf6, kf7);
        // 无线循环 + 反向转圈
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.setAutoReverse(true);

        // 这个动画也可以使用完成监听方法实现，多个时间轴触发播放

        hBox.getChildren().addAll(tb1, tb2, tb3, tb4);
        AnchorPane an = new AnchorPane();
        an.getChildren().addAll(hBox, rec);
        AnchorPane.setTopAnchor(hBox, 100d);
        AnchorPane.setLeftAnchor(hBox, 100d);
        AnchorPane.setTopAnchor(rec, 300d);
        AnchorPane.setLeftAnchor(rec, 100d);
        Scene scene = new Scene(an);
        primaryStage.setScene(scene);
        primaryStage.setTitle("ToggleButton");
        primaryStage.setWidth(1200d);
        primaryStage.setHeight(800d);
        primaryStage.setAlwaysOnTop(true);
        primaryStage.show();

        addAction(timeline, tb1, tb2, tb3, tb4);
    }

    private void addAction(Timeline timeline, ToggleButton tb1, ToggleButton tb2, ToggleButton tb3, ToggleButton tb4) {
        tb1.setOnAction(event -> {
            timeline.play();
        });

        tb2.setOnAction(event -> {
            timeline.pause();
        });

        tb3.setOnAction(event -> {
            timeline.stop();
        });

        tb4.setOnAction(event -> {

        });
    }
}
